package com.phospherus.user.repository

import com.phospherus.common.repository.BaseRepository
import com.phospherus.user.entity.User
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param

/**
 * @author songjinlong
 * @version 1.0
 * @since 2017/11/8
 */
interface UserRepository : BaseRepository<User, Long> {
    fun findByAccountAndPassWord(userName: String, passWord: String): User?
    @Modifying
    @Query(value = "update user set email= :email,phone= :phone where id= :userId", nativeQuery = true)
    fun modifyUserInfo(@Param("userId") userId: Long, @Param("email") email: String, @Param("phone") phone: String)

    @Modifying
    @Query(value = "update user set pass_word= :passWord where id= :userId", nativeQuery = true)
    fun modifyPassWord(@Param("userId") userId: Long?, @Param("passWord") passWord: String?)

    @Query("select u.* from user u,role_user ru where ru.role_id= :roleId and u.id=ru.user_id", nativeQuery = true)
    fun getBindingsUsers(@Param("roleId") roleId: Long): List<User>

    @Query("select u.* from user u,role_user ru where ru.role_id!= :roleId and u.id=ru.user_id group by u.id", nativeQuery = true)
    fun getUnBindingsUsers(@Param("roleId") roleId: Long): List<User>

    @Query("select u.* from role_user ru, user u where ru.role_id= :roleId and u.id=ru.user_id", nativeQuery = true)
    fun getUsersByRoleId(@Param("roleId") roleId: Long): List<User>
}